package com.modular.json;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class JackJsonUtil {
    private static final Logger logger = LoggerFactory.getLogger(JackJsonUtil.class);

    public JackJsonUtil() {
    }


    public static byte[] toJsonAsBytes(Object data) {
        try {
            return getInstance().writeValueAsBytes(data);
        } catch (JsonProcessingException var2) {
            logger.error("toJsonAsBytes()数据转换异常: {}", var2.getMessage(), var2);
            return null;
        }
    }

    public static String toJson(Object data) {
        try {
            return getInstance().writeValueAsString(data);
        } catch (Exception var2) {
            logger.error("对象转换Json数据失败: {}", var2.getMessage(), var2);
            return null;
        }
    }

    public static String toJsonIgnoreNull(Object data) {
        try {
            return getIgnoreInstance().writeValueAsString(data);
        } catch (JsonProcessingException var2) {
            logger.error("对象转换Json数据失败: {}", var2.getMessage(), var2);
            return null;
        }
    }

    public static <T> T parse(byte[] bytes, Class<T> valueType) {
        try {
            return getInstance().readValue(bytes, valueType);
        } catch (IOException var3) {
            logger.error("Json字符串: {} 转换对象失败: {}", new Object[]{bytes, var3.getMessage(), var3});
            return null;
        }
    }

    public static <T> T parse(byte[] bytes, TypeReference<T> typeReference) {
        try {
            return getInstance().readValue(bytes, typeReference);
        } catch (IOException var3) {
            logger.error("bytes数组: {} 转换对象失败: {}", new Object[]{bytes, var3.getMessage(), var3});
            return null;
        }
    }

    public static <T> T parse(InputStream in, Class<T> valueType) {
        try {
            return getInstance().readValue(in, valueType);
        } catch (IOException var3) {
            logger.error("InputStream读取数据转换对象失败: {}", var3.getMessage(), var3);
            return null;
        }
    }

    public static <T> T parse(InputStream in, TypeReference<T> typeReference) {
        try {
            return getInstance().readValue(in, typeReference);
        } catch (IOException var3) {
            logger.error("InputStream读取数据转换对象失败: {}", var3.getMessage(), var3);
            return null;
        }
    }

    public static <T> T parse(String json, Class<T> valueType) {
        try {
            return getInstance().readValue(json, valueType);
        } catch (IOException var3) {
            logger.error("Json字符串: {} 转换对象失败: {}", new Object[]{json, var3.getMessage(), var3});
            return null;
        }
    }

    public static <T> T parse(String json, TypeReference<T> typeReference) {
        try {
            return getInstance().readValue(json, typeReference);
        } catch (IOException var3) {
            logger.error("Json字符串: {} 转换对象失败: {}", new Object[]{json, var3.getMessage(), var3});
            return null;
        }
    }

    public static <T> T parseIgnoreUnknown(byte[] bytes, Class<T> valueType) {
        try {
            return getIgnoreInstance().readValue(bytes, valueType);
        } catch (IOException var3) {
            logger.error("bytes数组: {} 转换对象失败: {}", new Object[]{bytes, var3.getMessage(), var3});
            return null;
        }
    }

    public static <T> T parseIgnoreUnknown(byte[] bytes, TypeReference<T> typeReference) {
        try {
            return getIgnoreInstance().readValue(bytes, typeReference);
        } catch (IOException var3) {
            logger.error("bytes数组: {} 转换对象失败: {}", new Object[]{bytes, var3.getMessage(), var3});
            return null;
        }
    }

    public static <T> T parseIgnoreUnknown(InputStream in, Class<T> valueType) {
        try {
            return getInstance().readValue(in, valueType);
        } catch (IOException var3) {
            logger.error("InputStream读取数据转换对象失败: {}", var3.getMessage(), var3);
            return null;
        }
    }

    public static <T> T parseIgnoreUnknown(InputStream in, TypeReference<T> typeReference) {
        try {
            return getIgnoreInstance().readValue(in, typeReference);
        } catch (IOException var3) {
            logger.error("InputStream读取数据转换对象失败: {}", var3.getMessage(), var3);
            return null;
        }
    }

    public static <T> T parseIgnoreUnknown(String json, Class<T> valueType) {
        try {
            return getIgnoreInstance().readValue(json, valueType);
        } catch (IOException var3) {
            logger.error("Json字符串: {} 转换对象失败: {}", new Object[]{json, var3.getMessage(), var3});
            return null;
        }
    }

    public static <T> T parseIgnoreUnknown(String json, TypeReference<T> typeReference) {
        try {
            return getIgnoreInstance().readValue(json, typeReference);
        } catch (IOException var3) {
            logger.error("Json字符串: {} 转换对象失败: {}", new Object[]{json, var3.getMessage(), var3});
            return null;
        }
    }

    public static JsonNode readTree(String json) {
        try {
            return getInstance().readTree(json);
        } catch (IOException var2) {
            logger.error("Json字符串: {} 转换JsonNode失败: {}", new Object[]{json, var2.getMessage(), var2});
            return null;
        }
    }

    public static JsonNode readTree(byte[] content) {
        try {
            return getInstance().readTree(content);
        } catch (IOException var2) {
            logger.error("转换JsonNode失败: {}", var2.getMessage(), var2);
            return null;
        }
    }

    public static JsonNode readTree(InputStream in) {
        try {
            return getInstance().readTree(in);
        } catch (IOException var2) {
            logger.error("转换JsonNode失败: {}", var2.getMessage(), var2);
            return null;
        }
    }

    public static JsonNode readTree(JsonParser jsonParser) {
        try {
            return (JsonNode)getInstance().readTree(jsonParser);
        } catch (IOException var2) {
            logger.error("转换JsonNode失败: {}", var2.getMessage(), var2);
            return null;
        }
    }

    public static <T> List<T> parseArray(String json, Class<T> valueType) {
        try {
            if (!StrUtil.startWith(json, String.valueOf('['))) {
                json = "[" + json + "]";
            }

            JavaType javaType = getInstance().getTypeFactory().constructParametricType(List.class, new Class[]{valueType});
            return (List)getInstance().readValue(json, javaType);
        } catch (IOException var3) {
            logger.error("Json字符串: {} 转换Array对象失败: {}", new Object[]{json, var3.getMessage(), var3});
            return null;
        }
    }

    public static <T> List<T> parseArrayIgnoreUnknown(String json, Class<T> valueType) {
        try {
            if (!StrUtil.startWith(json, String.valueOf('['))) {
                json = "[" + json + "]";
            }

            JavaType javaType = getIgnoreInstance().getTypeFactory().constructParametricType(List.class, new Class[]{valueType});
            return (List)getIgnoreInstance().readValue(json, javaType);
        } catch (IOException var3) {
            logger.error("Json字符串: {} 转换Array对象失败: {}", new Object[]{json, var3.getMessage(), var3});
            return null;
        }
    }

    public static ObjectMapper getInstance() {
        return JacksonHolder.INSTANCE;
    }

    public static ObjectMapper getIgnoreInstance() {
        return JacksonIgnoreHolder.INSTANCE;
    }

    private static class JacksonHolder {
        private static final ObjectMapper INSTANCE = new ObjectMapper();

        private JacksonHolder() {
        }
    }

    private static class JacksonIgnoreHolder {
        private static final ObjectMapper INSTANCE = new JacksonObjectMapper();

        private JacksonIgnoreHolder() {
        }
    }

    public static class JacksonObjectMapper extends ObjectMapper {
        private static final long serialVersionUID = -6771509863783187190L;

        public JacksonObjectMapper() {
            super.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, false).setSerializationInclusion(JsonInclude.Include.NON_NULL);
        }
    }
}
